#include "postdata.h"
#include<U8g2lib.h>
#include <Wire.h>
#include "mysql_aliyun.h"

// Onenet的APIKEY
String APIKEY = "DCDN=HCYV84=u218lrdrGWvcuA4=";
// Onenet的域名
String HOST = "api.heclouds.com";
//若无DNS，使用Server服务端的IP 183.230.40.33
const char* host = "183.230.40.33";
String url = "http://api.heclouds.com/devices/611697416/datapoints?type=3";
const char OneNetServer[] = "api.heclouds.com";

#define DHTPIN  2   
#define DHTTYPE DHT11

//用于存放温湿度；
float humidity;
float temperature;
float light;

//存放时间和天气信息；
char showtime1[11];
char showtime2[9];
char showtianqi[100];
DHT dht(DHTPIN, DHTTYPE);  //温湿度在GPIO2;

void read_nd(char te[5]){
  dht.begin();
  humidity = dht.readHumidity();//读湿度
  temperature = dht.readTemperature();//读温度(摄氏度)
  if(temperature>25){
    digitalWrite(0, HIGH);
  }else{
    digitalWrite(0, LOW);
  }
  if(humidity<25){
    digitalWrite(15, HIGH);
  }else{
    digitalWrite(15, LOW);
  }
  Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
  tsl.begin();
  sensor_t sensor;
  tsl.getSensor(&sensor);
  delay(500);
  
  tsl.enableAutoRange(true);          
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); 
  
  sensors_event_t event;
  tsl.getEvent(&event);
  light = event.light;
  if(light<25){
    digitalWrite(16, HIGH);
  }else{
    digitalWrite(16, LOW);
  }
  char tem[5] = "12.4", hum[4] = "77", lig[4]="11";
  
  tem[5] = (char)temperature;
  hum[4] = (char)humidity;
  lig[4]=(char)light;
  Send_info(tem, hum, lig);
  delay(500);
  
}

// 通过HTTPClient类发送POST请求
void HTTPClient_POST(char te[5]) {
    WiFiClient client;
    HTTPClient http;
    const uint16_t port = 80;
    if (!client.connect(host, port)) {
        Serial.println("connection failed");
        delay(1000);
        return;
    } else {
        Serial.println("WiFiClient connect ok");
        if(http.begin(client, url)) {
            Serial.print("Remote Server Connect OK!");
            Serial.printf("[HTTP} connect ok\n");
            http.addHeader("api-key", APIKEY);
            http.addHeader("Host", HOST);
            http.addHeader("Content-Length", "10");
            http.addHeader("Connection", "close");
            
            read_nd("30");    //获取到温湿度、光照强度数据；
        
            delay(500);
            String postdata = "{\"" + String("Humidity") + "\":" + String(humidity) + ",\"" + String("Temperature") + "\":" + String(temperature)+",\""  + String("light") + "\":" + String(light)+ "}" ;
            
            Serial.print("Message that you send-->");
            Serial.println(postdata);

            int httpCode = http.POST(postdata);

            if (httpCode > 0) {
                // 如果状态码正常
                if (httpCode == HTTP_CODE_OK) {
                    Serial.print("HTTP状态码->");
                    Serial.println(httpCode);
                    String payload = http.getString();
                    Serial.print("Message that Server return -->");
                    Serial.println(payload);

                }
            }
            else {
                Serial.println("HTTP code < 0");
                Serial.println(httpCode);
            }
            http.end();
        } else {
            Serial.printf("[HTTP} Unable to connect\n");
        }

    }

}
void read_t(char te[50]){
  HTTPClient httpClient;
  httpClient.begin(te);
  int httpCode = httpClient.GET();
  if (httpCode == HTTP_CODE_OK) {
    String responsePayload = httpClient.getString();
    //Serial.println(responsePayload);
    if(te=="http://quan.suning.com/getSysTime.do"){
      for(int i=0;i<19;i++){
        Serial.print(responsePayload[13+i]);
        if(i<11)
        {
          showtime1[i]=responsePayload[13+i];
        }
        else
        {
          showtime2[i-11]=responsePayload[13+i];
        }
      }
      Serial.println(" ");
      showtime1[10]='\0';
      showtime2[8]='\0';
    }else if(te=="http://api.jirengu.com/getWeather.php?city=淄博"){
      for(int i=316;i<380;i++)
      {
        showtianqi[i-316]=responsePayload[i];
      }
      Serial.println(showtianqi);
    }

    
  }else {
    Serial.println("Server Respose Code：");
    Serial.println(httpCode);
  }
  httpClient.end();

}

void xianshi(char te[5]){
  //OLED屏的SDA和SCL分别连接开发板4和5；
  U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
  u8g2.begin();
  u8g2.firstPage();
  do{
      u8g2.setFont(u8g2_font_ncenB08_tr); 
      u8g2.drawStr(0,10,showtime1);
      u8g2.drawStr(60,10,showtime2);
      u8g2.setFont(u8g2_font_ncenB08_tr);
      u8g2.setCursor(0, 25);
      u8g2.print("T");
      u8g2.setFont(u8g2_font_unifont_t_symbols);
      u8g2.drawUTF8(20, 25, "：");
      u8g2.setFont(u8g2_font_ncenB08_tr); 
      u8g2.setCursor(40, 25);
      u8g2.print(temperature);
      u8g2.setFont(u8g2_font_ncenB08_tr);
      u8g2.setCursor(0, 40);
      u8g2.print("H");
      u8g2.setFont(u8g2_font_unifont_t_symbols);
      u8g2.drawUTF8(20, 40, "：");
      u8g2.setFont(u8g2_font_ncenB08_tr); 
      u8g2.setCursor(40, 40);
      u8g2.print(humidity);
      u8g2.setFont(u8g2_font_ncenB08_tr);
      u8g2.setCursor(0, 55);
      u8g2.print("Light");
      u8g2.setFont(u8g2_font_unifont_t_symbols);
      u8g2.drawUTF8(20, 55, "：");
      u8g2.setFont(u8g2_font_ncenB08_tr); 
      u8g2.setCursor(40, 55);
      u8g2.print(light);
      
    }while(u8g2.nextPage());
}
